package com.dxp.plugins;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.service.CacheManager;
import com.apache.cache.service.impl.LoadCacheFactory;
import com.apache.cache.util.Validator;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname ZunlRoutesSynCachePluginImpl
 * @Description 路由存储缓存同步
 * @Date 2020/3/19 11:35
 * @Created by fpc
 */
public class ZunlRoutesSynCachePluginImpl implements CustomMethodPlugin {
    @Override
    public Object beforeInvoke(Map<String, Object> param) {
//        param.put()
        String routeType = String.valueOf(param.get("dxGwProcessControl.routeType"));
        String url = String.valueOf(param.get("dxGwProcessControl.url"));
        String serviceId = String.valueOf(param.get("dxGwProcessControl.serverId"));
        if("url".equals(routeType)){
            if(Validator.isNull(url)){
                throw new BusinessException("请填写URL后提交。");
            }
            param.put("dxGwProcessControl.s_serviceId","is null");
        }else{
            if(Validator.isNull(serviceId)){
                throw new BusinessException("请填写serviceId后提交。");
            }
            param.put("dxGwProcessControl.s_url","is null");
        }
        Map<String,Object> whereMap ;
        String path = String.valueOf(param.get("dxGwProcessControl.predicates"));
        String modelTypes = String.valueOf(param.get("modelTypes"));
        whereMap = new HashMap<>();
        if(Validator.isNotNull(serviceId)){
            whereMap.put("dxGwProcessControl.wo_serviceId",serviceId);
            whereMap.put("dxGwProcessControl.wo_predicates",path);
        }else{
            whereMap.put("dxGwProcessControl.w_predicates",path);
        }
        ResultEntity resultEntity = IusPluginUtil.getInstance().select("obj", "s_dxGwProcessControl", whereMap);
        if(!Validator.isEmpty(resultEntity.getEntity())){
            if(modelTypes.startsWith("i_")){
                throw new BusinessException("【注册服务Id】或【过滤路径】已存在，请修改后重新提交注册。");
            }else{
                String w_infoId = String.valueOf(param.get("dxGwProcessControl.w_infoId"));
                Map entity = (Map) resultEntity.getEntity();
                String infoId = String.valueOf(entity.get("infoId"));
                if(!infoId.equals(w_infoId)){
                    throw new BusinessException("【注册服务Id】或【过滤路径】已存在，请修改后重新提交注册。");
                }


            }
        }

        param.put("afterMethodKey", "zunlRoutesSynCachePlugin");
        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> param) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> param) {
        CacheManager zuulRoutes = LoadCacheFactory.getInstance().getCacheManager("zuul.routes");

        String infoId = "";
        String modelTypes = String.valueOf(param.get("modelTypes"));
        Map<String,Object> whereMap = new HashMap<>();
        String enabled = String.valueOf(param.get("dxGwProcessControl.enabled"));
        if("1".equals(enabled)){
            if(modelTypes.startsWith("i_")){
                Map<String,String> idMap = (Map<String,String>)param.get("resultIds");
                infoId = idMap.get("dxGwProcessControl_infoId");
            }else {
                infoId = String.valueOf(param.get("dxGwProcessControl.w_infoId"));
            }
            whereMap.put("dxGwProcessControl.w_infoId",infoId);
            ResultEntity entity = IusPluginUtil.getInstance().select("obj", "s_dxGwProcessControl", whereMap);
            if(!Validator.isEmpty(entity.getEntity())){
                zuulRoutes.createCacheObject(infoId,entity.getEntity());
            }
        }else {
            if(modelTypes.startsWith("u_")){
                infoId = String.valueOf(param.get("dxGwProcessControl.w_infoId"));
                zuulRoutes.removeCacheObject(infoId);
            }
        }
        return null;
    }
}
